package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.IStudentService;
import com.hzit.utilt.ResponseUtil;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import sun.swing.StringUIClientPropertyKey;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")   //   http://localhost:9000/student?cmd=list
public class StudServlet extends HttpServlet {
	private IStudentService studentService;

	//1. 初始化得到当前studentService对象
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		studentService = wac.getBean(IStudentService.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		String cmd = req.getParameter("cmd");
		if(StrUtil.isNotBlank(cmd)){
			if ("list".equals(cmd)) {
				list(req,resp);             // 列表学生
			}
			if("add".equals(cmd)){
				add(req,resp);              // 添加学生
			}
			if("update".equals(cmd)){
				update(req,resp);           // 修改学生
			}
			if("delete".equals(cmd)){       // 删除学生
				delete(req,resp);
			}
		}
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		try {
			//1. 得到要删除的学生编号
			String sid = req.getParameter("sid");
			//2. 通过数据库删除学生
			studentService.delete(sid);
			//3. 输出信息到前端
			ResponseUtil.out(resp,R.ok(null,"删除成功!"));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("删除失败!"));
		}

	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			//1. 得到修改的学生数据
			ObjectMapper objectMapper = new ObjectMapper();
			Student student = objectMapper.readValue(req.getInputStream(), Student.class);
			//2. 将其修改到数据库
			studentService.update(student);
			//3. 响应回前端
			ResponseUtil.out(resp,R.ok(null,"修改学生成功！"));
		} catch (IOException e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("修改学生失败！"));
		}
	}

	private void add(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		try {
			//1. 得到添加的学生数据
			ObjectMapper objectMapper = new ObjectMapper();
			Student student = objectMapper.readValue(req.getInputStream(), Student.class);
			//2. 将其添加到数据库
			studentService.insert(student);
			//3. 响应回前端
			ResponseUtil.out(resp,R.ok(null,"添加学生成功！"));
		} catch (IOException e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("添加学生失败！"));
		}
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			//1. 查询得到学生列表
			List<Student> studentList = studentService.findAll();
			//2. 将学生列表发送到前端
			ResponseUtil.out(resp, R.ok(studentList));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp, R.fail("查询学生列表失败！"));
		}

	}
}
